package cn.lu.joke.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import cn.lu.joke.util.UnitUtils;

public class ChoiceVideoMatrixView extends View {

    private static final String TAG = "ChoiceMatrixView";

    private int scale = (int) this.getResources().getDisplayMetrics().density;  //屏幕像素密度
    private float borderHeight;   //总高
    private float borderWith; //总宽
    private float selectWidth = 200; //边框长度
    private int RECT_BORDER_WITH = 2 * scale; //长方形框框粗

    private int minBorderLength = UnitUtils.dp2px(30);

    private int range = 8 * scale;

    //四个点坐标
    public static float[] four_corner_positions = new float[8];

    private static boolean MOVE_OR_ZOOM_STATE = true; //移动或缩放状态， true 为移动

    public ChoiceVideoMatrixView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setFocusable(true);
        this.setFocusableInTouchMode(true);
        init();
    }

    /**
     * 初始化布局
     * @param changed
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);


        borderHeight = this.getHeight();
        borderWith = this.getWidth();

        //初始化四个点的坐标
        four_corner_positions[0] = RECT_BORDER_WITH;   //左上 x
        four_corner_positions[1] = 0; //左上 y
        four_corner_positions[2] = borderWith / 2;   //右上 x
        four_corner_positions[3] = 0; //右上 y
        four_corner_positions[4] = 0;   //左下 x
        four_corner_positions[5] = borderHeight;  //左下 y
        four_corner_positions[6] = borderWith / 2;    //右上 x
        four_corner_positions[7] = borderHeight;  //右上 y
    }

    /**
     * RECT_CORNER_WITH = 6
     * RECT_BORDER_WITH  =3
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();  //初始化画笔
        //画边框的画笔
        paint.setColor(Color.GREEN);    //颜色
        paint.setStrokeWidth(RECT_BORDER_WITH);    //宽度
        paint.setAntiAlias(true);   //抗锯齿
        paint.setStyle(Paint.Style.STROKE); //设置空心
        canvas.drawRect(
                four_corner_positions[0],
                four_corner_positions[1],
                four_corner_positions[6],
                four_corner_positions[7], paint);

        Path path = new Path();
        path.moveTo(four_corner_positions[0] - RECT_BORDER_WITH, four_corner_positions[1]);
        path.lineTo(four_corner_positions[4] - RECT_BORDER_WITH, four_corner_positions[5]);
        paint.setStrokeWidth(6);
        canvas.drawPath(path, paint);

        path.moveTo(four_corner_positions[2] - RECT_BORDER_WITH, four_corner_positions[3]);
        path.lineTo(four_corner_positions[6] -RECT_BORDER_WITH, four_corner_positions[7]);
        canvas.drawPath(path, paint);
    }

    private int lastX = 0;  //上次按下的X位置
    private int lastY = 0;  //上次按下的Y位置
    private int offsetX = 0;    //X轴偏移量
    private int offsetY = 0;    //Y轴偏移量
    static int point = -1;// 用户按下的点
    private int POINT_STATE = -1; //判断用户是缩小还是放大 1放大 0缩小

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                point = isInTheCornerCircle(event.getX(), event.getY());
                if (point != -1) {
                    //开始缩放操作
                    MOVE_OR_ZOOM_STATE = false; //设置false为缩放状态
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                offsetX = x - lastX;
                offsetY = y - lastY;
                //判断当前是扩大还是缩小操作
                judgementXandY();
                //限定移动范围
                //移动状态：只有在移动状态下才能移动
                if (MOVE_OR_ZOOM_STATE) {
                    /*for (int i = 0; i < four_corner_positions.length -1; i += 2) {
                        four_corner_positions[i] += offsetX;
                        four_corner_positions[i + 1] += offsetY;

                        invalidate();
                    }*/
                    break;
                } else {
                    //在缩放状态下
                    //按住某一个点，该点的坐标改变，其他2个点坐标跟着改变，对点坐标不变
                    max = Math.abs(offsetX) >= Math.abs(offsetY) ? Math.abs(offsetX) : Math.abs(offsetY);

                    selectWidth = four_corner_positions[2] - four_corner_positions[0];
                    //只有在缩小操作才进行边界范围判断，不能太小
                    if (POINT_STATE == 0) {
                        if (selectWidth < minBorderLength) {
                            return true;
                        }
                    } else if (POINT_STATE == 1) {
                        //放大操作时进行边界不能太大判断
                        if (point == 0 && four_corner_positions[0] <= RECT_BORDER_WITH) {
                            return true;
                        }
                        if (point == 2 && four_corner_positions[2] >= borderWith) {
                            return true;
                        }
                        if (selectWidth >= borderWith) {
                            return true;
                        }
                    }

                    //改变坐标
                    changgeEightCoodinatePosition(point, offsetX);
                }
                lastX = x;
                lastY = y;
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                MOVE_OR_ZOOM_STATE = true; //回归为默认的移动状态
                break;
        }
        return super.onTouchEvent(event);
    }

    private void init() {

    }

    /**
     * POINT_STATE 为1放大， 0缩小
     */
    private void judgementXandY() {
        switch (point) {
            case 0:
                if ((offsetX <= 0 && offsetY <= 0) || (offsetX <= 0 && offsetY >= 0)) {
                    POINT_STATE = 1;//扩大
                } else {
                    POINT_STATE = 0;//缩小
                }
                break;
            case 2:
                if ((offsetX <= 0 && offsetY >= 0) || (offsetX <= 0 && offsetY <= 0)) {
                    POINT_STATE = 0;
                } else {
                    POINT_STATE = 1;
                }
                break;
        }
    }

    static int max;

    /**
     * 缩小扩大缩放方法
     * 根据用户传来的点改变其他点的坐标
     * 按住某一个点，该点的坐标改变，其他2个点坐标跟着改变，对边的点坐标不变
     * 点阵示意：
     * 0  4  1
     * 6     5
     * 2  7  3
     *
     * @param point   用户按的点
     * @param offsetX X轴偏移量
     */
    private void changgeEightCoodinatePosition(int point, int offsetX) {
        switch (point) {
            case 0:
            case 2:
                if(offsetX < 0){
                    //2缩小0放大
                    four_corner_positions[point] -= max;
                    four_corner_positions[point + 4] -= max;
                    if (point == 0 && four_corner_positions[point] < 0) {
                        four_corner_positions[point] = RECT_BORDER_WITH;
                        four_corner_positions[point + 4] = RECT_BORDER_WITH;
                    }
                } else{
                    //0缩小2放大
                    four_corner_positions[point] += max;
                    four_corner_positions[point + 4] += max;
                    if (point == 2 && four_corner_positions[point] > borderWith) {
                        four_corner_positions[point] = borderWith;
                        four_corner_positions[point + 4] = borderWith;
                    }
                }
                break;
        }

    }

    /**
     * 判断按下的点在圆圈内
     *
     * @param x 按下的X坐标
     * @param y 按下的Y坐标
     * @return 返回按到的是哪个点, 没有则返回-1
     * 点阵示意：
     * 0   1
     * 2   3
     */
    private int isInTheCornerCircle(float x, float y) {
        for (int i = 0; i < four_corner_positions.length -5; i += 2) {
            float a = four_corner_positions[i];
            float b = four_corner_positions[i + 1];
            float c = four_corner_positions[i + 5];
            if (x > a - range && x < a + range && y > b && y < c) {
                return i;
            }
        }
        return -1;
    }

    public void setSelectWidth(float selectWidth) {
        this.selectWidth = selectWidth;
    }

    public interface onImageDetailsSizeChangged {
        void onBorderSizeChangged(int x, int y, int length);
    }

    public onImageDetailsSizeChangged onImageDetailsSizeChanggedl;

    public void setonImageDetailsSizeChangged(onImageDetailsSizeChangged onImageDetailsSizeChangged) {
        this.onImageDetailsSizeChanggedl = onImageDetailsSizeChangged;
    }

}

